package com.quectel.grid.controller.village;

import com.quectel.business.common.grid.GridMemberBindResourcesBusiness;
import com.quectel.constant.core.grid.GridConstant;
import com.quectel.core.module.grid.dto.GridBindingDto;
import com.quectel.core.module.grid.dto.GridMemberDto;
import com.quectel.core.module.grid.service.GridBindingService;
import com.quectel.core.module.village.dto.VillageBuildingDto;
import com.quectel.core.module.village.service.VillageBuildingService;
import com.quectel.grid.controller.BaseController;
import com.quectel.grid.interceptor.LoginGridMemberSession;
import com.quectel.grid.util.SessionHolder;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 在这里编写说明
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-02-16 09:45:00
 */
@RestController
@RequestMapping("villageBuilding")
@Tag(name = "VillageBuildingController", description = "网格实有房间相关api")
public class VillageBuildingController extends BaseController {

    @DubboReference
    private VillageBuildingService buildingService;
    @DubboReference
    private GridBindingService gridBindingService;

    @Autowired
    private GridMemberBindResourcesBusiness gridMemberBindResourcesBusiness;

    /**
     * 通过网格id 获取房屋单元列表
     *
     * @param gridId 网格id
     * @return
     */
    @GetMapping("queryBuildingsByGridId")
    @Operation(summary = "获取选中网格绑定楼栋信息")
    public Response<List<VillageBuildingDto>> queryBuildingsByGridId(
            @Parameter(description = "网格id") @RequestParam(required = false) Long gridId) {

        LoginGridMemberSession loginGridUserSession = SessionHolder.getLoginGridUserSession();
        GridMemberDto gridMember = loginGridUserSession.getGridMember();
        List<VillageBuildingDto> result = new ArrayList<>();
        List<GridBindingDto> bindingDtoList = new ArrayList<>();
        // 没选中网格 那么传自己拥有的网格的
        if (gridId != null) {
            bindingDtoList = gridBindingService.selectListByGridIdAndSourceType(gridId, GridConstant.SourceType.BUILDING.getType());
            if (CollectionUtils.isNotEmpty(bindingDtoList)) {
                for (GridBindingDto gridBinding : bindingDtoList) {
                    VillageBuildingDto building = buildingService.selectById(gridBinding.getSourceId());
                    if (building != null) {
                        result.add(building);
                    }
                }
            }
        } else {
            // 网格员拥有网格
            List<Long> gridIds = gridMemberBindResourcesBusiness.getGridIdByGridMember(gridMember);
            for (Long recordGridId : gridIds) {
                bindingDtoList = gridBindingService.selectListByGridIdAndSourceType(recordGridId, GridConstant.SourceType.BUILDING.getType());
                if (CollectionUtils.isNotEmpty(bindingDtoList)) {
                    for (GridBindingDto gridBinding : bindingDtoList) {
                        VillageBuildingDto building = buildingService.selectById(gridBinding.getSourceId());
                        if (building != null) {
                            result.add(building);
                        }
                    }
                }
            }
        }
        return Response.<List<VillageBuildingDto>>ok().wrap(result);
    }

}

